<?php
/**
 * User: Viet Anh
 * Date: 13/06/2014
 * Time: 01:15
 */

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_choice.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_match.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_roulette_game.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_roulette_map.php";

class api_roulette
{
    //Roulette

    public static function get_win_choice_id($roulette_id) {
        $dao_match = new dao_match();
        $match = $dao_match->get_by_id($roulette_id);
        if (!empty($match)) {
            return $match['win_choice_id'];
        }
    }
    
    public static function get_sub_match_id_by_game_id_and_parent_id($game_id, $parent_id) {
        $dao_match = new dao_match();
        $match = $dao_match->get_match_by_game_id_and_parent_id($game_id, $parent_id);
        if (!empty($match)) {
            return $match['id'];
        }
    }
    
    public static function save_roulette_result($roulette_id, $roulette_map) {
        $dao_match = new dao_match();
        
        for ($index = 1; $index <= 6; $index++) {
            $game_id = $index;
            $win_choice_id = self::get_choice_id_by_game_id_and_roulette_map($game_id, $roulette_map);
            $match = $dao_match->update_roulette_win_choice_id($roulette_id, $win_choice_id, $game_id);
            $sub_match = $dao_match->get_match_by_game_id_and_parent_id($game_id, $roulette_id);
            api_bet_calculators::finalize_match($sub_match['id'], $win_choice_id);
        }
        
        $match = $dao_match->update_win_choice_id_by_id($roulette_id, $roulette_map['id']);
        return TRUE;
    }
    
    public static function get_choice_id_by_game_id_and_roulette_map($game_id, $roulette_map) {
        if ($game_id == 1) {
            if ($roulette_map['color'] === "red") {
                return 3;
            } else if ($roulette_map['color'] === "black") {
                return 4;
            }
        } else if ($game_id == 2) {
            if ($roulette_map['number'] % 2 == 0) {
                return 5;
            } else {
                return 6;
            }
        } else if ($game_id == 3) {
            if (1 <= $roulette_map['number'] && $roulette_map['number'] <= 12) {
                return 7;
            } else if ($roulette_map['number'] <= 24) {
                return 8;
            } else if ($roulette_map['number'] <= 36) {
                return 9;
            }
        } else if ($game_id == 4) {
            if (1 <= $roulette_map['number'] && $roulette_map['number'] <= 18) {
                return 10;
            } else if ($roulette_map['number'] <= 36) {
                return 11;
            }
        } else if ($game_id == 5) {
            if ($roulette_map['column'] == 1) {
                return 12;
            } else if ($roulette_map['column'] == 2) {
                return 13;
            } else if ($roulette_map['column'] == 3) {
                return 14;
            }
        } else if ($game_id == 6) {
            return $roulette_map['number'] + 14;
        }
        return 0;
    }
    
    public static function convert_map($roulette_id, $result) {
        $dao_match = new dao_match();
        
        $match = $dao_match->update_win_choice_id($roulette_id, $parent_id);
        if (!empty($match)) {
            return $match['id'];
        }
    }
    
    public static function get_all_roulettes()
    {
        $dao_match = new dao_match();

        return $dao_match->get_all_roulettes();
    }

    public static function get_game_by_game_id($game_id)
    {
        $dao_roulette_game = new dao_roulette_game();
        return $dao_roulette_game->find($game_id);
    }
    
    public static function get_all_game_choices($game_id)
    {
        $dao_choice = new dao_choice();

        return $dao_choice->get_all_by_game_id($game_id);
    }

    public static function get_first_game_name()
    {
        $dao_roulette_game = new dao_roulette_game();
        $game = $dao_roulette_game->find(1);
        if (!empty($game)) {
            return $game['name'];
        }
        return "";
    }

    public static function get_all_games()
    {
        $dao_roulette_game = new dao_roulette_game();

        return $dao_roulette_game->get_list();
    }

    public static function get_roulette_by_id($roulette_id)
    {
        $dao_match = new dao_match();
        return $dao_match->get_roulette_by_id($roulette_id);
    }

    public static function insert_roulette($name, $start_date, $end_date)
    {
        $dao_roulette = new dao_match();

        return $dao_roulette->save_roulette(0, $name, $start_date, $end_date);
    }

    public static function update_roulette($id, $name, $start_date, $end_date)
    {
        $dao_roulette = new dao_match();
        $win_choice_id = api_roulette::get_win_choice_id($id);
        if (lib_date::convert_to_datetime($end_date, "Y-m-d H:i:s") <= lib_date::get_datetime_now()
                && empty($win_choice_id)) {
            $result = api_roulette::random_result();
            api_roulette::save_roulette_result($id, $result);
        }
        return $dao_roulette->save_roulette($id, $name, $start_date, $end_date);
    }

    public static function delete_roulette($id)
    {
        $dao_roulette = new dao_match();

        return $dao_roulette->delete($id);
    }

    // roulette map
    public static function random_result() {
        $dao_roulette_map = new dao_roulette_map();
        $list = $dao_roulette_map->get_all();
        if (!empty($list) && count($list) > 0) {
            $m = mt_rand(0, count($list));
            return $list[$m];
        }
        return 0;
    }

    public static function get_roulette_map_by_map_id($map_id) {
        $dao_roulette_map = new dao_roulette_map();
        return $dao_roulette_map->get_by_id($map_id);
    }
    
    //Game
    public static function get_list_roulette_game()
    {
        $dao_game = new dao_roulette_game();

        return $dao_game->get_list();
    }

    //Game Choice
    public static function get_list_roulette_game_choice()
    {
        $dao_choice = new dao_choice();

        return $dao_choice->get_all_roulette_choice();
    }
}